#include "widget.h"
#include "ui_widget.h"
#include <QString>
#include "loginset.h"
#include "admin.h"
#include "client.h"
#include "maintenance.h"
#include "mysqlserver.h"
#include <QMessageBox>


mysqlServer mysql;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_login_clicked()
{

    QString uname = ui->unameTxt->text(); // 从输入框获取用户名
    QString upass = ui->upassTxt->text(); // 从输入框获取密码

    if(mysql.connectMysql())
    {
        qDebug() << "数据库连接成功";
    }
    else
    {
        qDebug() << "数据库连接失败" << mysql.db.lastError().text();
        QMessageBox::critical(this,"连接失败","数据库连接失败，请检查IP和端口号是否正确");
        return;
    }

    if(uname == "" || upass == "")
    {
        QMessageBox::warning(this,"登录失败","用户名或密码不能为空");
        return;
    }


    QSqlQuery query;
    if(query.exec("SELECT uname,upwd,perm FROM user"))
    {
        int flat = 1;
        while (query.next())
        {
            QString name = query.value("uname").toString();
            QString pass = query.value("upwd").toString();
            int perm = query.value("perm").toInt();
            qDebug() << name;
            qDebug() << pass;
            qDebug() << perm;

            if( name == uname) // 该用户存在
            {
                if(pass == upass) // 密码正确
                {
                    if(perm == 1)
                    {
                        admin *aL = new admin(uname,upass,mysql.SERVER, mysql.PORT);
                        aL->show();
                        this->close();
                    }
                    if(perm == 2)
                    {
                        client *cL = new client(uname,upass,mysql.SERVER, mysql.PORT);
                        cL->show();
                        this->close();
                    }
                    if(perm == 3)
                    {
                        maintenance *mL = new maintenance(uname,upass,mysql.SERVER, mysql.PORT);
                        mL->show();
                        this->close();
                    }
                }
                else // 密码错误
                {
                    QMessageBox::warning(this,"登录失败","密码错误");
                }


            }
            if(name == uname) // 判断用户是否存在
            {
                flat = 0;
            }
        }
        if(flat)
        {
            // 不存在
            QMessageBox::warning(this,"登录失败","该用户不存在");
            // 或强制激活窗口
            // L->raise();
            // L->activateWindow();
        }
    }
    else
    {
        qDebug() << "失败";
    }
}



void Widget::on_ipSet_clicked()
{
    loginSet *L = new loginSet(mysql.SERVER,mysql.PORT,this); // 指定父对象
    L->show();

    // // 强制激活
    // L->raise();
    // L->activateWindow();
}


void Widget::on_enroll_clicked()
{
    QString uname = ui->unameTxt->text(); // 从输入框获取用户名
    QString upass = ui->upassTxt->text(); // 从输入框获取密码

    qDebug() << mysql.SERVER;
    qDebug() << mysql.PORT;

    if(mysql.connectMysql())
    {
        qDebug() << "数据库连接成功";
    }
    else
    {
        qDebug() << "数据库连接失败" << mysql.db.lastError().text();
        QMessageBox::critical(this,"连接失败","数据库连接失败，请检查ip地址和端口号");
        return;
    }

    if(uname == "" || upass == "")
    {
        QMessageBox::warning(this,"登录失败","用户名或密码不能为空");
        return;
    }

    QSqlQuery query;


    query.prepare("INSERT INTO user (uname,upwd) VALUES (?, ?)"); // 预处理语句
    query.addBindValue(uname);
    query.addBindValue(upass);
    if(!query.exec())
    {
        qDebug() << "插入失败：" << query.lastError().text();
        QMessageBox::warning(this,"注册提示","注册失败,用户名已存在");
    }
    else
    {
        query.prepare("INSERT INTO member_info (client_name,client_grade) VALUES (?,VIP1)"); // 预处理语句
        query.addBindValue(uname);
        qDebug() << "插入成功，影响行数：" << query.numRowsAffected();
        QMessageBox::information(this,"注册提示","注册成功");
    }

}

